package com.ruoyi.project.enable;

import lombok.extern.slf4j.Slf4j;
import org.springframework.aop.Advisor;
import org.springframework.aop.TargetSource;
import org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator;
import org.springframework.aop.support.DefaultIntroductionAdvisor;
import org.springframework.beans.BeansException;

import java.util.Arrays;

@Slf4j
public class AutoEnhanceProxyCreator extends AbstractAutoProxyCreator {

    private final String[] excludes;
    private final Advisor advisor = new DefaultIntroductionAdvisor(new AutoEnhanceAdvice());

    public AutoEnhanceProxyCreator(boolean useJdkProxy, String[] excludes) {
        this.excludes = excludes;
        setProxyTargetClass(!useJdkProxy);
    }

    @Override
    protected Object[] getAdvicesAndAdvisorsForBean(Class<?> aClass, String beanName, TargetSource targetSource) throws BeansException {
        return new Object[]{advisor};
    }


    @Override
    protected boolean shouldSkip(Class<?> beanClass, String beanName) {
        return Arrays.asList(excludes).contains(beanClass.getName())
                || !beanName.endsWith("Controller");

    }
}
